#!/bin/bash

## Copyright (c) 2013, PoiXson, Mattsoft
## <http://poixson.com/>
## <http://mattsoft.net/>
## 
## Permission to use, copy, modify, and/or distribute this software for any
## purpose with or without fee is hereby granted, provided that the above
## copyright notice and this permission notice appear in all copies.
## 
## THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
## WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
## MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
## ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
## WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
## ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
## OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


if [ -z "$DEBUG" ]; then
	source $(dirname $0)/_common.sh
fi


title "Creating a new repo mirror.."
echo "Caution: This will delete an existing mirror from the filesystem, if one already exists."
newline
echo "Mirrors base: ${PXN_REPOMIRRORS}"
newline
newline


echo "[ http://<<reponame>>.googlecode.com/svn/trunk/ ]"
read -p "Please enter just the source repo name: " answer
if [ -z "$answer" ]; then
	exit
fi
reponame=${answer}
sourcerepo="http://${answer}.googlecode.com/svn/trunk/"
echo "Using source: ${sourcerepo}"
newline
newline


echo "[ git@github.com:<<account>>/reponame.git ]"
read -p "Please enter the destination repo account: " answer
if [ -z "$answer" ]; then
	exit
fi
destrepo="git@github.com:${answer}"
newline
newline


echo "[ git@github.com:account/<<reponame>>.git ]"
read -p "Please enter the destination repo name, or blank to use same as source: " answer
if [ -z "$answer" ]; then
	answer=${reponame}
fi
destrepo="${destrepo}/${answer}.git"
echo "Using source: ${sourcerepo}"
echo "Using destination: ${destrepo}"
newline
newline


if [ -d "${PXN_REPOMIRRORS}/${reponame}.git/" ]; then
	echo "Existing: ${PXN_REPOMIRRORS}/${reponame}.git/"
	read -p "Delete existing mirror? [y/N] " answer
	if [ "$answer" != "y" ]; then
		if [ "$answer" != "Y" ]; then
			exit
		fi
	fi
fi
rm -Rvf "${PXN_REPOMIRRORS}/${reponame}.git/"
echo -n "Creating mirror."; sleepdot
newline
newline


echo git svn clone "${sourcerepo}" "${PXN_REPOMIRRORS}/${reponame}.git"
git svn clone "${sourcerepo}" "${PXN_REPOMIRRORS}/${reponame}.git"
newline
newline
echo git remote add origin ${destrepo}
cd "${PXN_REPOMIRRORS}/${reponame}.git/"
git remote add origin ${destrepo}
newline
newline


echo -n "chown."; sleepdot
newline
chown user:user -Rc "${PXN_REPOMIRRORS}/${reponame}.git/"


newline
echo "Finished creating repo mirror"
newline


#// newmirror.sh reponame http://svnrepo git@github.com:lorenzop/project.git


#//sh newmirror.sh GrowControl http://growcontrol.googlecode.com/svn/trunk/ git@github.com:lorenzop/GrowControl.git
#//sh newmirror.sh ProSiteManager http://prositemanager.googlecode.com/svn/trunk/ git@github.com:lorenzop/ProSiteManager.git
#//sh newmirror.sh WebAuctionPlus http://webauctionplus.googlecode.com/svn/trunk/ git@github.com:lorenzop/WebAuctionPlus.git
#//sh newmirror.sh WebAuctionPlus-Sandbox http://webauctionplus.googlecode.com/svn/sandbox/ git@github.com:lorenzop/WebAuctionPlusSandbox.git
#//sh newmirror.sh WeBookPlus http://webookplus.googlecode.com/svn/trunk/ git@github.com:lorenzop/WeBookPlus.git


#// Note: If you are mirroring a live subversion repository and new branches are added you will need to monitor this yourself and set up a new tracking branch in your GIT repository using the following within the repository directory.
#//git branch --track remotes/branchname branchname

# git svn clone $2 $1.git
# cd $BASE_DIR/$1.git
# git remote add origin $3
# cd $BASE_DIR


# dump repos
# if [ "$1" == "dump" ]; then
#	sh dump-repo.sh GrowControl http://growcontrol.googlecode.com/svn/
#	sh dump-repo.sh WebAuctionPlus http://webauctionplus.googlecode.com/svn/
#	sh dump-repo.sh WeBookPlus http://webookplus.googlecode.com/svn/
#	sh dump-repo.sh ProSiteManager http://prositemanager.googlecode.com/svn/
# else
#	echo
#	echo
#	echo Skipping - Not dumping repos!
# fi


# echo chown/chmod
# chown user:user -c *.sh
# chown user:user -c _dumps/*.dump
# chmod 775 -c *.sh
# chmod 775 -c _dumps/*.dump
# echo

